home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Disk / moni / FileX-src.lha / FileX-src / cursor.c < prev    next >
C/C++ Source or Header  |  2003-10-21  |  9KB  |  446 lines

  1. #include "allprotos.h"
  2. #include "FileXStructs.h"
  3.  
  4. void CursorMarkOn( struct DisplayData *DD )
  5. {
  6.     if( DD->Flags & DD_MARK )
  7.         MarkOn( DD );
  8.     else
  9.         CursorOn( DD );
  10. }
  11.  
  12. void CursorMarkOff( struct DisplayData *DD )
  13. {
  14.     if( DD->Flags & DD_MARK )
  15.         MarkOff( DD );
  16.     else
  17.         CursorOff( DD );
  18. }
  19.  
  20. void ExpandBlock( long x1, long x2, struct DisplayData *DD )
  21. {
  22.     long s1, s2;
  23.  
  24.     x1 -= DD->SPos * DD->BPR;
  25.     x2 -= DD->SPos * DD->BPR;
  26.  
  27.     if( x1 < x2 )
  28.     {
  29.         s1 = x1;
  30.         s2 = x2;
  31.     }
  32.     else
  33.     {
  34.         s1 = x2;
  35.         s2 = x1;
  36.     }
  37.  
  38.     SetAPen( DD->Wnd->RPort, BlockAPen );
  39.     SetBPen( DD->Wnd->RPort, BlockBPen );
  40.     DisplayPart( s1, s2, DD );
  41. }
  42.  
  43. void ShrinkBlock( long x1, long x2, struct DisplayData *DD )
  44. {
  45.     long s1, s2;
  46.  
  47.     x1 -= DD->SPos * DD->BPR;
  48.     x2 -= DD->SPos * DD->BPR;
  49.  
  50.     if( x1 < x2 )
  51.     {
  52.         s1 = x1;
  53.         s2 = x2;
  54.     }
  55.     else
  56.     {
  57.         s1 = x2;
  58.         s2 = x1;
  59.     }
  60.  
  61.     SetAPen( DD->Wnd->RPort, NormalAPen );
  62.     SetBPen( DD->Wnd->RPort, NormalBPen );
  63.     DisplayPart( s1, s2, DD );
  64. }
  65.  
  66. /*
  67.  * void SetCursor( long Pos, struct DisplayData *DD )
  68.  *
  69.  * Setzt den Cursor an die Stelle Pos(in Bytes). Der Cursor muß schon
  70.  * irgendwo anders stehen.
  71.  */
  72.  
  73. void SetCursor( long Pos, struct DisplayData *DD )
  74. {
  75. /*    Printf("SetCursor(%ld) %ld,\n", Pos, DD->CPos);*/
  76.  
  77.     if( DD->FD->Len == 0 ) return;
  78.  
  79.         /* HexEditPos auf obere 4Bits setzten */
  80.  
  81.     DD->Flags &= ~DD_HEXEDITPOS;
  82.  
  83.         /* Von Pos = 0 ins negative =>nichts machen */
  84.  
  85.     if(( DD->CPos == 0 ) && ( Pos < 0 )) return;
  86.  
  87.         /* Cursor nach hinten und stehen wir in der letzten Zeile? */
  88.  
  89.     if(( Pos > DD->CPos ) && (( DD->CPos / DD->BPR ) == ( DD->FD->Len / DD->BPR )))
  90.     {
  91.             /* Soll der Cursor an das Ende der Zeile? */
  92.             /* Wenn ja, dann Cursor an das Ende des Files setzen */
  93.             /* Wenn nein und der Cursor außerhalb stehen soll, abbrechen */
  94.  
  95.         if( Pos % DD->BPR == DD->BPR - 1 )
  96.             Pos = DD->FD->Len - 1;
  97.         else
  98.             if( Pos >= DD->FD->Len )
  99.                 return;
  100.     }
  101.  
  102.         /* Falls der Cursor nicht im Filebereich, anpassen */
  103.  
  104.     if( Pos < 0 )
  105.         Pos = ( Pos + DD->BPR * DD->Zeilen ) % DD->BPR;
  106.     else
  107.         if( Pos >= DD->FD->Len )
  108.         {
  109.             if( DD->FD->Len % DD->BPR <= Pos % DD->BPR )
  110.                 Pos = Pos % DD->BPR + ( DD->FD->Len / DD->BPR - 1 ) * DD->BPR;
  111.             else
  112.                 Pos = Pos % DD->BPR + DD->FD->Len / DD->BPR * DD->BPR;
  113.         }
  114.  
  115.         /* Gleiche Position und keine Verschiebung => abbrechen */
  116.  
  117.     if( Pos == DD->CPos )
  118.     if((( Pos >= DD->SPos * DD->BPR + DD->ScrollRand * DD->BPR ) &&
  119.             ( Pos < ( DD->SPos + DD->Zeilen - DD->ScrollRand ) * DD->BPR )) ||
  120.         ( Pos < DD->ScrollRand * DD->BPR ) ||
  121.         ( Pos > ( DD->FD->Len - DD->ScrollRand * DD->BPR )))
  122.             return;
  123.  
  124.         /* Im Markierungsmodus dieses Ex-Riesenbaby abarbeiten */
  125.         /* Version 2.1: Total geändert und wieder länger!! */
  126.  
  127.     if( DD->Flags & DD_MARK )
  128.     {
  129.         long PosZeile = Pos / DD->BPR;
  130.  
  131.             /* Falls neue CursorPos außerhalb, in den neuen Bereich scrollen */
  132.  
  133.         if( PosZeile < DD->SPos + DD->ScrollRand )
  134.         {
  135.             long diff = PosZeile - ( DD->SPos + DD->ScrollRand );
  136.             if( DD->SPos + diff < 0 )
  137.                 diff = -DD->SPos;
  138. #ifdef DEBUG
  139.             PutStr("SetCursor: scrolling up\n");
  140.             Printf("Diff = %ld\n", diff );
  141. #endif
  142.             if((!diff)||( -diff > DD->Zeilen ))
  143.             {
  144.                 DD->SPos += diff;
  145.                 DD->CPos = Pos;
  146.  
  147.                 RedrawDisplay( DD );
  148.  
  149.             }
  150.             else
  151.             {
  152.                 long tp = Pos;
  153.  
  154.                 if( tp < DD->SPos * DD->BPR )
  155.                     tp = DD->SPos * DD->BPR;
  156.  
  157.                 if( tp >= DD->MPos )
  158.                 {
  159.                     if( tp != Pos ) tp--;
  160.                     ShrinkBlock( DD->CPos, tp + 1, DD );
  161.                 }
  162.                 else if( DD->CPos <= DD->MPos )
  163.                 {
  164. //                    ExpandBlock( tp, DD->CPos - 1, DD );
  165.                     ExpandBlock( tp, DD->CPos, DD );
  166.                 }
  167.                 else
  168.                 {
  169.                     ShrinkBlock( DD->CPos, DD->MPos + 1, DD );
  170.                     ExpandBlock( DD->MPos - 1, tp, DD );
  171.                 }
  172.  
  173.                 DD->SPos += diff;
  174.                 DD->CPos = Pos;
  175.                     
  176.                 MoveDisplay( diff, DD );
  177.                 RedrawPart( 0, - diff - 1, DD );
  178.                 SetScrollerGadget( DD );
  179.             }
  180.         }
  181.         else if( PosZeile >= DD->SPos + DD->Zeilen - DD->ScrollRand )
  182.         {
  183.             long diff = PosZeile - ( DD->SPos + DD->Zeilen - DD->ScrollRand - 1 );
  184.             long ZeilenImFile = ( DD->FD->Len + DD->BPR - 1 ) / DD->BPR;
  185. #ifdef DEBUG
  186.             PutStr("SetCursor: scrolling down\n");
  187.             Printf("Diff = %ld\n", diff );
  188. #endif
  189.             if( DD->SPos + diff + DD->Zeilen >  ZeilenImFile )
  190.             {
  191.                 if( ZeilenImFile < DD->Zeilen )
  192.                 {
  193.                     diff = 0;
  194.                 }
  195.                 else
  196.                 {
  197.                     diff = ZeilenImFile - ( DD->SPos + DD->Zeilen );
  198.                 }
  199.             }
  200.             if((!diff)||( diff > DD->Zeilen ))
  201.             {
  202.                 DD->SPos += diff;
  203.                 DD->CPos = Pos;
  204.  
  205.                 RedrawDisplay( DD );
  206.             }
  207.             else
  208.             {
  209.                 long tp = Pos;
  210.  
  211.                 if( tp >= ( DD->SPos + DD->Zeilen ) * DD->BPR )
  212.                     tp = ( DD->SPos + DD->Zeilen ) * DD->BPR - 1;
  213.  
  214.                 if( tp <= DD->MPos )
  215.                 {
  216.                     if( tp != Pos ) tp++;
  217.  
  218.                     ShrinkBlock( DD->CPos, tp - 1, DD );
  219.                 }
  220.                 else if( DD->CPos >= DD->MPos )
  221.                 {
  222. //                    ExpandBlock( tp, DD->CPos + 1, DD );
  223.                     ExpandBlock( tp, DD->CPos, DD );
  224.                 }
  225.                 else
  226.                 {
  227.                     ShrinkBlock( DD->CPos, DD->MPos - 1, DD );
  228.                     ExpandBlock( DD->MPos + 1, tp, DD );
  229.                 }
  230.  
  231.                 
  232.  
  233.                 DD->SPos += diff;
  234.                 DD->CPos = Pos;
  235.  
  236.                 MoveDisplay( diff, DD );
  237.                 RedrawPart( DD->Zeilen - diff, DD->Zeilen - 1, DD );
  238.                 SetScrollerGadget( DD );
  239.             }
  240.         }
  241.         else
  242.         {
  243. #ifdef DEBUG
  244.             PutStr("SetCursor: all conditions failed\n");
  245. #endif
  246.                 /* Es fand keine Verschiebung statt, wir müssen aber */
  247.                 /* den Block erweitern oder kürzen */
  248.  
  249.             if( DD->CPos == DD->MPos )
  250.             {
  251. #ifdef DEBUG
  252.                 PutStr("SetCursor: DD->CPos == DD->MPos\n");
  253. #endif
  254.                 if( Pos < DD->CPos )
  255.                 {
  256.                     ExpandBlock( DD->CPos - 1, Pos, DD );
  257.                 }
  258.                 else
  259.                 {
  260.                     ExpandBlock( DD->CPos + 1, Pos, DD );
  261.                 }
  262.             }
  263.             else if( DD->CPos > DD->MPos )
  264.             {
  265. #ifdef DEBUG
  266.                 PutStr("SetCursor: DD->CPos > DD->MPos\n");
  267. #endif
  268.                 if( Pos > DD->CPos )
  269.                 {
  270. //                    ExpandBlock( DD->CPos + 1, Pos, DD );
  271.                     ExpandBlock( DD->CPos, Pos, DD );
  272.                 }
  273.                 else if( Pos >= DD->MPos )
  274.                 {
  275.                     ShrinkBlock( DD->CPos, Pos + 1, DD );
  276.                 }
  277.                 else
  278.                 {
  279.                     ShrinkBlock( DD->MPos + 1, DD->CPos, DD );
  280.                     ExpandBlock( DD->MPos - 1, Pos, DD );
  281.                 }
  282.             }
  283.             else
  284.             {
  285. #ifdef DEBUG
  286.                 PutStr("SetCursor: DD->CPos < DD->MPos\n");
  287. #endif
  288.                 if( Pos < DD->CPos )
  289.                 {
  290. //                    ExpandBlock( DD->CPos - 1, Pos, DD );
  291.                     ExpandBlock( DD->CPos, Pos, DD );
  292.                 }
  293.                 else if( Pos <= DD->MPos )
  294.                 {
  295.                     ShrinkBlock( DD->CPos, Pos - 1, DD );
  296.                 }
  297.                 else
  298.                 {
  299.                     ShrinkBlock( DD->MPos - 1, DD->CPos, DD );
  300.                     ExpandBlock( DD->MPos + 1, Pos, DD );
  301.                 }
  302.             }
  303.  
  304.             DD->CPos = Pos;
  305.         }
  306.     }
  307.     else
  308.     {
  309.         long PosZeile = Pos / DD->BPR;
  310.  
  311.         CursorOff( DD );
  312.  
  313.             /* Falls eine Verschiebung überhaupt möglich ist */
  314.  
  315.         if( DD->FD->Len > DD->Zeilen * DD->BPR )
  316.         {
  317.                 /* Falls Cursor im unteren oder oberen DD->ScrollRand steht, */
  318.                 /*  Display verschieben */
  319.  
  320.             if( PosZeile < DD->SPos + DD->ScrollRand )
  321.             {
  322.                 long diff = PosZeile - ( DD->SPos + DD->ScrollRand );
  323.  
  324.                 if( DD->SPos + diff < 0 )
  325.                     diff = -DD->SPos;
  326. #ifdef DEBUG
  327.                 Printf("Diff=%ld, PosZeile=%ld\n", diff, PosZeile );
  328. #endif
  329.                 DD->SPos += diff;
  330.                 DD->CPos = Pos;
  331.  
  332.                 if( diff )
  333.                 if( -diff > DD->Zeilen )
  334.                 {
  335.                     RedrawDisplay( DD );
  336.                 }
  337.                 else
  338.                 {
  339.                     MoveDisplay( diff, DD );
  340.                     RedrawPart( 0, - diff - 1, DD );
  341.                     SetScrollerGadget( DD );
  342.                 }
  343.             }
  344.             else
  345.             if( Pos >= ( DD->SPos + DD->Zeilen - DD->ScrollRand ) * DD->BPR )
  346.             {
  347.                 long diff = PosZeile - ( DD->SPos + DD->Zeilen - DD->ScrollRand - 1 );
  348.                 long ZeilenImFile = ( DD->FD->Len + DD->BPR - 1 ) / DD->BPR;
  349. #ifdef DEBUG
  350.                 Printf("Diff=%ld, PosZeile=%ld\n", diff, PosZeile );
  351. #endif
  352.                 if( DD->SPos + diff + DD->Zeilen >  ZeilenImFile )
  353.                 {
  354.                     if( ZeilenImFile < DD->Zeilen )
  355.                     {
  356.                         diff = 0;
  357.                     }
  358.                     else
  359.                     {
  360.                         diff = ZeilenImFile - ( DD->SPos + DD->Zeilen );
  361.                     }
  362.                 }
  363.  
  364.                 DD->SPos += diff;
  365.                 DD->CPos = Pos;
  366.  
  367.                 if( diff )
  368.                 if( diff > DD->Zeilen )
  369.                 {
  370.                     RedrawDisplay( DD );
  371.                 }
  372.                 else
  373.                 {
  374.                     MoveDisplay( diff, DD );
  375.                     RedrawPart( DD->Zeilen - diff, DD->Zeilen - 1, DD );
  376.                     SetScrollerGadget( DD );
  377.                 }
  378.             }
  379.         }
  380.  
  381.         DD->CPos = Pos;
  382. //        CursorOn( DD );
  383.     }
  384.     UpdateStatusZeile( DD );
  385.     CursorOn(DD);
  386. }
  387.  
  388. void __inline MoveCursorX( WORD Steps, struct DisplayData *DD )
  389. {
  390.     SetCursor( DD->CPos + Steps, DD );
  391. }
  392.  
  393. void __inline MoveCursorY( WORD Steps, struct DisplayData *DD )
  394. {
  395.     if( DD->CPos + DD->BPR * Steps < 0 ) SetCursor( DD->CPos % DD->BPR, DD );
  396.     else SetCursor( DD->CPos + DD->BPR * Steps, DD );
  397. }
  398.  
  399. void __inline SetCursorX( WORD Pos, struct DisplayData *DD )
  400. {
  401.     SetCursor( DD->CPos / DD->BPR + Pos, DD );
  402. }
  403.  
  404. void __inline SetCursorY( WORD Pos, struct DisplayData *DD )
  405. {
  406.     SetCursor( DD->CPos % DD->BPR + Pos * DD->BPR, DD );
  407. }
  408.  
  409. void __inline SetCursorStart( struct DisplayData *DD )
  410. {
  411.     SetCursor( 0, DD );
  412.     SetScrollerGadget(DD);
  413. }
  414.  
  415. void __inline SetCursorEnd( struct DisplayData *DD )
  416. {
  417.     SetCursor( DD->FD->Len - 1, DD );
  418.     SetScrollerGadget(DD);
  419. }
  420.  
  421. void __inline SetCursorStartOfLine( struct DisplayData *DD )
  422. {
  423.     SetCursor( DD->CPos / DD->BPR * DD->BPR, DD );
  424. }
  425.  
  426. void __inline SetCursorEndOfLine( struct DisplayData *DD )
  427. {
  428.     SetCursor( DD->CPos / DD->BPR * DD->BPR + DD->BPR - 1, DD );
  429. }
  430.  
  431. void __inline SetCursorAltLeft( struct DisplayData *DD )
  432. {
  433.     if( DD->DisplaySpaces != 8 )
  434.         SetCursor((( DD->CPos - 1 ) & ( ~(( 1L << DD->DisplaySpaces ) - 1 ))), DD );
  435.     else
  436.         SetCursorStartOfLine( DD );
  437. }
  438.  
  439. void __inline SetCursorAltRight( struct DisplayData *DD )
  440. {
  441.     if( DD->DisplaySpaces != 8 )
  442.         SetCursor(( DD->CPos & ( ~(( 1L << DD->DisplaySpaces ) - 1 ))) + ( 1L << DD->DisplaySpaces ), DD );
  443.     else
  444.         SetCursorEndOfLine( DD );
  445. }
  446.